package org.hzero.exam.app.service.impl;

import org.hzero.exam.app.service.HodrSoLineService;
import org.hzero.exam.domain.entity.HodrItem;
import org.hzero.exam.domain.entity.HodrSoHeader;
import org.hzero.exam.domain.entity.HodrSoLine;
import org.hzero.exam.domain.repository.HodrItemRepository;
import org.hzero.exam.domain.repository.HodrSoHeaderRepository;
import org.hzero.exam.infra.mapper.HodrSoLineMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 马旭阳
 * @date2019/8/6
 */
@Service
public class HodrSoLineServiceImpl implements HodrSoLineService {
    @Autowired
    private HodrSoLineMapper soLineMapper;
    @Autowired
    private HodrSoHeaderRepository soHeaderRepository;
    @Autowired
    private HodrItemRepository itemRepository;
    @Override
    public void insert(HodrSoLine hodrSoLine) throws Exception{
        HodrSoLine soLine = soLineMapper.selectBySoHeaderIdAndLineNumber(hodrSoLine.getSoHeaderId(),hodrSoLine.getLineNumber());
        HodrSoHeader soHeader=soHeaderRepository.selectByPrimaryKey(hodrSoLine.getSoHeaderId());
        HodrItem item = itemRepository.selectByPrimaryKey(hodrSoLine.getItemId());
        if (soLine==null) {
            if (soHeader!=null){
                if (item!=null) {
                    soLineMapper.insert(hodrSoLine);
                }
            }
        }
            new Exception("不好意思，出错了");
    }


    @Override
    public void delete(HodrSoLine hodrSoLine) {

        soLineMapper.delete(hodrSoLine);
    }

    @Override
    public void update(HodrSoLine hodrSoLine) throws Exception{
        HodrSoHeader soHeader=soHeaderRepository.selectByPrimaryKey(hodrSoLine.getSoHeaderId());
        HodrItem item = itemRepository.selectByPrimaryKey(hodrSoLine.getItemId());
        if (soHeader!=null) {
            if (item != null) {
                soLineMapper.updateByPrimaryKey(hodrSoLine);
            }
        }
        new Exception("不好意思，出错了");
    }
}
